-- NewRarePanel

local RareRender = import(".renders.RareRender")
local NewListAdapterTool = require("app.widget.NewListAdapterTool")
local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewRarePanel))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._rareInfoPanel = nil -- 宝物详情UI

	display.uiAddClick(self._ui["Panel/Panel_rare/Panel_1"], function()
		local data = GD:queryRestrictedRewardShowById(102)
		local pos = self._ui["Panel/Panel_rare/Panel_1"]:convertToWorldSpace(cc.p(0,0))
		pos.x = pos.x - 320
		pos.y = pos.y + 175
		executeFunc(self._onOperate, "RareRender_explain", { data=data, pos=pos })
	end)

	display.uiAddClick(self._ui["Panel/Panel_rare/Panel_2"], function()
		local data = GD:queryRestrictedRewardShowById(3)
		local pos = self._ui["Panel/Panel_rare/Panel_2"]:convertToWorldSpace(cc.p(0,0))
		pos.x = pos.x - 320
		pos.y = pos.y + 230
		executeFunc(self._onOperate, "RareRender_explain", { data=data, pos=pos })
	end)

	display.uiAddClick(self._ui["Panel/Panel_rare/Panel_3"], function()
		local data = GD:queryRestrictedRewardShowById(4)
		local pos = self._ui["Panel/Panel_rare/Panel_3"]:convertToWorldSpace(cc.p(0,0))
		pos.x = pos.x - 320
		pos.y = pos.y + 230
		executeFunc(self._onOperate, "RareRender_explain", { data=data, pos=pos })
	end)

	self._ui["Panel/Panel_rare/icon"]:setTouchEnabled(true)

	display.uiAddClick(self._ui["Panel/Panel_rare/icon"], function()
		self:_cleanCharmRedPoint()
		
		local data = GD:queryRestrictedRewardShowById(101)
		local pos = self._ui["Panel/Panel_rare/icon"]:convertToWorldSpace(cc.p(0,0))
		pos.x = pos.x - 320
		pos.y = pos.y + 150
		executeFunc(self._onOperate, "RareRender_explain", { data=data, pos=pos })
	end)

	
	self._ui["Panel/Panel_rare/btn_doubt"]:addClickEventListener(function()
		self:_cleanCharmRedPoint()
		local data = GD:queryRestrictedRewardShowById(101)
		local pos = self._ui["Panel/Panel_rare/btn_doubt"]:convertToWorldSpace(cc.p(0,0))
		pos.x = pos.x - 320
		pos.y = pos.y + 150
		executeFunc(self._onOperate, "RareRender_explain", { data=data, pos=pos })
	end)

	self._ui["Panel/Panel_rare/Panel_tick/selected_btn"]:setTouchEnabled(true)
	local isHide = UD.setting:getIsHideBagRareEntry()
	self._ui["Panel/Panel_rare/Panel_tick/selected_btn"]:setBright(isHide)
	self._ui["Panel/Panel_rare/Panel_tick/selected_btn"]:addClickEventListener(function(event) 
		local isHide = UD.setting:getIsHideBagRareEntry()
		isHide = not isHide
		self._ui["Panel/Panel_rare/Panel_tick/selected_btn"]:setBright(isHide)
		UD.setting:setHideBagRareEntry(isHide)	
		if isHide then
            UIHandler:removeDesktopFunctionId(Constant.FUNC_TYPE_PACK_TREASURE)
        else
            UIHandler:addDesktopFunctionId(Constant.FUNC_TYPE_PACK_TREASURE, true, nil)
        end
	end)
end

function ClassRef:_cleanCharmRedPoint()
	UD:clearCharmNumNew()
	UD.redPoint:setStateByKey(RedPointConstant.Bag_NewCharm, false)
end

function ClassRef:getScrollView()
	return --self._rareList
end

function ClassRef:refreshView()
	local ownNum, usedToday = UD.player:getCharmUsedInfo()
	local canUsedToday = tonumber( GD:getSystemPar( "charmdailymaxcostamount" ) ) -- 今日可使用上限
	local maxNum = tonumber( GD:getSystemPar( "charmmaxamount" ) ) -- 可持有上限

	local canUseNum = canUsedToday - usedToday
	self._ui["Panel/Panel_rare/num"]:setString("x"..(ownNum or "0"))
	self._ui["Panel/Panel_rare/use_num"]:setString( string.format(L("CharmTodayCanUseNum"), canUseNum, canUsedToday) )

	-- 地下城宝箱
	local maxNum1_1 = tonumber( GD:getSystemPar( "dungeonBoxOpenLimited", 10 ) ) -- 每日开启上限
	local num1_1 = UD:getLuckBoxRestTimesByType(Enums.BoxType.dungeon)
	local str1_1 = table.concat( {num1_1, "/", maxNum1_1 } )
	--self._ui["Panel/Panel_rare/Panel_1/num_1"]:setString(str1_1)
	self._ui["Panel/Panel_rare/Panel_1/num_2"]:setVisible(false)
	self._ui["Panel/Panel_rare/Panel_1/bg_num2"]:setVisible(false)
	
	-- 竞技宝箱
	local maxNum2_1 = tonumber( GD:getSystemPar( "tourBoxOpenLimited", 10 ) ) -- 每日开启上限
	local str2_1 = table.concat( { UD:getLuckBoxRestTimesByType(Enums.BoxType.pvp), "/", maxNum2_1 } )
	-- local maxNumCanHold = tonumber( GD:getSystemPar( "tourBoxAccumulateLimited", 10 ) ) -- 可持有上限
	local haveNum2 = app:sendMsg("PvpModel", "getPvpBoxNum")
	-- if haveNum2 > maxNumCanHold then
	-- 	haveNum2 = maxNumCanHold
	-- end
	-- if haveNum2 > canUseNum then
	-- 	haveNum2 = canUseNum
	-- end
	--self._ui["Panel/Panel_rare/Panel_2/num_1"]:setString(str2_1)
	if haveNum2 == 0 then
		self._ui["Panel/Panel_rare/Panel_2/num_2"]:setVisible(false)
		self._ui["Panel/Panel_rare/Panel_2/bg_num2"]:setVisible(false)
	else
		self._ui["Panel/Panel_rare/Panel_2/num_2"]:setVisible(true)
		self._ui["Panel/Panel_rare/Panel_2/bg_num2"]:setVisible(true)
		self._ui["Panel/Panel_rare/Panel_2/num_2"]:setString(haveNum2)
	end

	-- 活动宝箱
	local maxNum3_1 = tonumber( GD:getSystemPar( "eventBoxDailyOpenLimited", 10 ) ) -- 每日开启上限
	local num3_1 = UD:getLuckBoxRestTimesByType(Enums.BoxType.eventBox)
	local str3_1 = table.concat( { num3_1, "/", maxNum3_1 } )
	local curActivityDungeonsPoint = UD:getActivityDungeonsPoint() -- 活动点数
	local oneBoxNeedPoint = GD:getSystemPar( "eventBoxPointRequired", 200 ) -- 一个包厢需要的点数
	local curAccumulateRewardNum = math.floor( curActivityDungeonsPoint/oneBoxNeedPoint ) -- 累积的个数
	-- local maxAccumulateRewardNum = tonumber( GD:getSystemPar( "eventBoxAccumulateLimited", 20 ) ) -- 累积的上限
	-- if curAccumulateRewardNum > maxAccumulateRewardNum then
	-- 	curAccumulateRewardNum = maxAccumulateRewardNum
	-- end
	-- if curAccumulateRewardNum > canUseNum then
	-- 	curAccumulateRewardNum = canUseNum
	-- end
	--self._ui["Panel/Panel_rare/Panel_3/num_1"]:setString(str3_1)
	if curAccumulateRewardNum == 0 then
		self._ui["Panel/Panel_rare/Panel_3/num_2"]:setVisible(false)
		self._ui["Panel/Panel_rare/Panel_3/bg_num2"]:setVisible(false)
	else
		self._ui["Panel/Panel_rare/Panel_3/num_2"]:setVisible(true)
		self._ui["Panel/Panel_rare/Panel_3/bg_num2"]:setVisible(true)
		self._ui["Panel/Panel_rare/Panel_3/num_2"]:setString(curAccumulateRewardNum)
	end
	self:refreshRedPoint()
end

function ClassRef:remove()

end

function ClassRef:registerOnOperate( cb )
	self._onOperate = cb
end


function ClassRef:refreshRedPoint()
	self._ui["Panel/Panel_rare/icon/dot"]:setVisible(UD.redPoint:getStateByKey(RedPointConstant.Bag_NewCharm))
end

return ClassRef
